package com.tool.emulator.common;

import com.tool.emulator.apple2.runtime.EmuState;
import com.tool.emulator.apple2.runtime.VirtualDeviceState;

/**
 * VirtualDevice.java @author tl 2011-6-17
 */
public abstract class VirtualDevice {
	protected final String name;
	protected volatile transient long last_accessing_time = 0;

	protected VirtualDevice(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public String toString() {
		return this.name;
	}

	protected void setAccessingTime() {
		this.last_accessing_time = System.currentTimeMillis();
	}

	public long getLastAccessingTime() {
		return this.last_accessing_time;
	}

	public boolean isBusy() {
		return System.currentTimeMillis() - this.getLastAccessingTime() < 500;
	}

	protected void deviceInit() throws Exception {
	}

	protected void deviceExit() throws Exception {

	}

	public void deviceSaveState(EmuState emuState) throws Exception {
		VirtualDeviceState virtualDeviceState = new VirtualDeviceState();
		virtualDeviceState.saveState(this);

		if (virtualDeviceState != null) {
			emuState.putVirtualDeviceState(this, virtualDeviceState);
		}

	}

	public void deviceLoadState(EmuState emuState) throws Exception {
		VirtualDeviceState virtualDeviceState = emuState.getVirtualDeviceState(this);
		virtualDeviceState.loadState(this);

	}
}
